package com.hxzy.controller;

import com.hxzy.common.domain.AjaxResult;
import com.hxzy.common.page.TableDataInfo;
import com.hxzy.dto.HoustStatusDTO;
import com.hxzy.entity.AdminUser;
import com.hxzy.entity.HouseStatus;
import com.hxzy.framework.controller.BaseController;
import com.hxzy.service.HouseStatusService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 类说明
 *
 * @author admin
 * @date 2021-09-08
 */
@Controller
@RequestMapping(value = "/admin")
public class HouseStatusController extends BaseController {

    @Autowired
    private HouseStatusService  houseStatusService;

    @GetMapping(value = "/house/status/list")
    public String showPage(Model model){
      return "house/status/list";
    }

    @ResponseBody
    @PostMapping(value = "/house/status/list")
    public TableDataInfo ajaxData(HoustStatusDTO  houstStatusDTO, HttpServletRequest request) {
        super.startPage(request);
        List<HouseStatus> houseStatusList = this.houseStatusService.search(houstStatusDTO);
        return super.getDataTable(houseStatusList);
    }


    /**
     * 显示新增页面
     * @param model
     * @return
     */
    @GetMapping(value = "/house/status/add")
    public String addPage(Model model){
        return "house/status/add";
    }

    @ResponseBody
    @PostMapping(value = "/house/status/add")
    public AjaxResult addData(HouseStatus houseStatus){
        AdminUser adminUser= (AdminUser) SecurityUtils.getSubject().getPrincipal();

          houseStatus.setCreateTime(new Date());
          houseStatus.setUpdateTime(new Date());
          houseStatus.setCreateBy(adminUser.getLoginUserName());
          houseStatus.setUpdateBy(adminUser.getLoginUserName());

          int count=this.houseStatusService.insert(houseStatus);
          return super.toAjax(count);
    }


    /**
     * 显示编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping(value = "/house/status/edit/{id}")
    public String editPage(@PathVariable(value = "id") Integer id, Model model){
        HouseStatus houseStatus = this.houseStatusService.selectByPrimaryKey(id);
        //把数据转给页面
        model.addAttribute("houseStatus", houseStatus);

        return "house/status/edit";
    }

    /**
     * 修改保存
     * @param houseStatus
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/house/status/edit")
    public AjaxResult editData(HouseStatus houseStatus){
        AdminUser adminUser= (AdminUser) SecurityUtils.getSubject().getPrincipal();

        houseStatus.setUpdateTime(new Date());
        houseStatus.setUpdateBy(adminUser.getLoginUserName());
        //一定要调用updateByPrimaryKeySelective 选择性更新，
        // updateByPrimaryKey 全部列更新，因为修改时修，新增时间和新增人未赋值
        int count=this.houseStatusService.updateByPrimaryKeySelective(houseStatus);
        return super.toAjax(count);
    }

    /**
     * 判断名称是否存在
     * @param id
     * @param name
     * @return 0代表可以，1代表不可用
     */
    @ResponseBody
    @GetMapping(value = "/house/status/exists")
    public int  existsName(Integer id, String name){
        int count = this.houseStatusService.existsName(id, name);
        return count;
    }

}
